package com.csw.dao;

import com.csw.bean.Weibo;
import com.csw.util.JDBCUtil;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


/**
 * 再类上增加Component注解，spring会自动创建这个类的对象
 *
 */

@Component
public class WeiboDao {

    /**
     * 通过编号查询微博
     *
     * @param id 微博编号
     * @return 微博内容
     */

    public Weibo queryWeiboById(Integer id) {

        Weibo weibo = null;

        //1、获取连接
        Connection con = JDBCUtil.getConnection();

        try {
            PreparedStatement stat = con.prepareStatement("select * from t_weibo where id=?");


            stat.setInt(1, id);


            //执行拆线呢
            ResultSet resultSet = stat.executeQuery();


            //解析数据
            if (resultSet.next()) {

                weibo = new Weibo();

                weibo.setId(id);

                weibo.setText(resultSet.getString("text"));
            }


        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        return weibo;
    }
}
